package core

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"gopkg.in/yaml.v2"
	"muziai-server/conf"
	"muziai-server/flags"
	"os"
)

func InitYaml() (c *conf.Config) {
	var fileName string
	switch flags.Options.Env {
	case "prod":
		fileName = "settings.yaml"
	case "dev":
		fileName = "settings_local.yaml"
	}
	byteData, err := os.ReadFile(fileName)
	if err != nil {
		panic(fmt.Sprintf("Read yaml file %s error--%s", fileName, err))
	}

	err = yaml.Unmarshal(byteData, &c)
	if err != nil {
		panic(fmt.Sprintf("yaml解析错误--%s", fileName, err))
	}

	fmt.Printf("yaml读取成功，当前环境为 %s\n", flags.Options.Env)
	return

}
func SetYaml(c *conf.Config) (err error) {
	var fileName string
	switch flags.Options.Env {
	case "prod":
		fileName = "settings.yaml"
	case "dev":
		fileName = "settings_local.yaml"
	}
	byteData, _ := yaml.Marshal(c)
	err = os.WriteFile(fileName, byteData, 0666)
	if err == nil {
		logrus.Infof("修改配置文件%s成功", fileName)
	}
	return err
}
